using System.Data;
using NUnit.Framework;
using PropertyExpression.ReactiveRelation.Test.Common;
using PropertyExpression.ReactiveRelation.Values;

namespace PropertyExpression.ReactiveRelation.Test
{
    [TestFixture]
    public class TemperatureTest : BaseDbTest
    {
        protected override void DbClean()
        {
            DataContextProviderHolder.Provider
                .Execute(context =>
                             {
                                 context.Temperatures.DeleteAll();

                                 context.SubmitChanges();
                             },
                         IsolationLevel.Serializable);
        }

        [Test]
        public void Test()
        {
            var calcStorage = new CalcStorage<object>(
                registrator =>
                registrator.Register<Temperature>(
                    binder =>
                        {
                            binder._(_ => _.Fahrenheit)(
                                it => it._(_ => _.Celsius)
                                          .Mult(1.8m.Const()).Add(32m.Const())
                                );

                            binder._(_ => _.Celsius)(
                                it => it._(_ => _.Fahrenheit)
                                          .Minus(32m.Const()).Div(1.8m.Const())
                                );
                        }
                    )
                );

            var temperature = new Temperature().Couple(
                CalcStorageExtensions.CreateCalcExecutor(() => calcStorage, () => null));

            temperature._(_ => _.Celsius)(100);
            Assert.AreEqual(100, temperature.Entity.Celsius);
            Assert.AreEqual(212, temperature.Entity.Fahrenheit);

            temperature._(_ => _.Fahrenheit)(32);
            Assert.AreEqual(0, temperature.Entity.Celsius);
            Assert.AreEqual(32, temperature.Entity.Fahrenheit);

            temperature._(_ => _.Celsius)(-40);
            Assert.AreEqual(-40, temperature.Entity.Celsius);
            Assert.AreEqual(-40, temperature.Entity.Fahrenheit);
        }
    }
}